//
//  CZQCycleChildHotVC.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/23.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQCycleChildHotVC.h"
#import <SDCycleScrollView.h>
#import "CZQCycleDetalVC.h"
#import "CZQCycleModel.h"
#import "CZQHomePageChileOneVC.h"
#import "FindeDetailViewController.h"
@interface CZQCycleChildHotVC ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate,DHCycleTableViewCellDelegate>
/**
 //数据源
 */
@property(nonatomic,strong)NSMutableArray *dataArr;

/**
 //轮播图
 */
@property(nonatomic,strong)NSMutableArray *photoDataArr;

/**
 tableView
 */
@property(nonatomic,weak)UITableView *tableView;

@property(nonatomic,assign)NSInteger index;

@property(nonatomic,assign)NSInteger pageSize;

@property(nonatomic,strong)NoDataView *noDataView;

@property(nonatomic,weak)SDCycleScrollView *cycleScrollView;
@end

@implementation CZQCycleChildHotVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.index = 1;
    
    [self loadDataCycle];
    
    
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    [self loadStartData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- 懒加载
-(NSMutableArray *)photoDataArr
{
    if (!_photoDataArr) {
        
        _photoDataArr = [NSMutableArray array];
        
    }
    
    return _photoDataArr;
}

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
        
    }
    
    return _dataArr;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        tableView.delegate=self;
        
        tableView.dataSource=self;
        
        
        tableView.estimatedRowHeight = 150;
        
        [self.view addSubview:tableView];
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self.view);
            
        }];
        
        //        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        //注册
        [tableView registerClass:[DHCycleTableViewCell class] forCellReuseIdentifier:@"cell"];
        
        
        _tableView = tableView;
        
        //tableHead
        SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(5, 5, kScreenWidth -10, kScreenWidth/5*3) delegate:self placeholderImage:PlaceHold];
        
        cycleScrollView.autoScroll = YES;
        
        self.cycleScrollView = cycleScrollView;
        
        [self.view addSubview:cycleScrollView];
        
        tableView.tableHeaderView = cycleScrollView;
        
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        footer.automaticallyHidden = YES;
        
        
        MyTableHead *mj_head = [MyTableHead headerWithRefreshingTarget:self refreshingAction:@selector(loadStartData)];
        tableView.mj_header = mj_head;
        tableView.mj_footer = footer;
        [tableView.mj_header beginRefreshing];
        
    }
    return _tableView;
}
#pragma mark 下拉
-(void)loadStartData{
    
    self.index = 1;
    
    self.pageSize = 20;
    
    [self loadData];
    
    [self loadDataCycle];
}

#pragma mark 上拉
-(void)loadMoreData{
    
    self.index = self.index + 1;
    
    [self loadData];
}

#pragma mark - 循环图
-(void)loadDataCycle{
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setObject:[NSString stringWithFormat:@"%d",1] forKey:@"page"];
    
    [params setObject:[NSString stringWithFormat:@"%d",100] forKey:@"rows"];
    
    
    [params setObject:[NSString stringWithFormat:@"%d",1] forKey:@"type"];
    
    
    
    
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    [HTTPTool GET:GET_ArticleList parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              NSLog(@"%@",responseObject);
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSArray *resultArray = [responseObject objectForKey:@"Hots"];
                  
                  NSMutableArray *picArrM = [NSMutableArray array];
                  
                  for (NSDictionary *dict in resultArray) {
                      
                      if ([[dict objectForKey:@"Banner"] isNull]) {
                          
                          continue;
                      }
                      
                      NSString *banner = [dict objectForKey:@"Banner"];
                      
                      [self.photoDataArr addObject:dict];
                      
                      [picArrM addObject: banner];
                      
                  }
                  
                  self.cycleScrollView.imageURLStringsGroup = picArrM;
                  
                  
              }
              
          }
     
          failure:^(NSError *error){
              
              [self.tableView.mj_header endRefreshing];
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}
-(void)loadData{
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setObject:[NSString stringWithFormat:@"%d",self.index] forKey:@"page"];
    
    [params setObject:[NSString stringWithFormat:@"%d",self.pageSize] forKey:@"rows"];
    
    [params setObject:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"UserID"]] forKey:@"VoteMemberID"];;
    
//    [params setObject:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"UserID"]] forKey:@"MemberID"];
//
//    [params setObject:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"MasterType"]] forKey:@"UserType"];
    
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    [HTTPTool GET:GET_Activity_List parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              NSLog(@"%@",responseObject);
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  
                  if (resultArray.count > 0) {
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshing];
                  }
                  else{
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshingWithNoMoreData];
                  }
                  
                  if (self.index == 1) {
                      [self.dataArr removeAllObjects];
                  }
                  
                  
                  
                  
                  for (NSDictionary *resultDic in resultArray) {
                      
                      CZQCycleModel *cycle = [CZQCycleModel mj_objectWithKeyValues:resultDic];
                      
                      [self.dataArr addObject:cycle];
                      
                  }
                  self.noDataView = [NoDataView shareInstance];
                  
                  [self.noDataView hideInView:self.view];
                  
                  if (self.dataArr.count == 0) {
                      
                      self.noDataView = [NoDataView shareInstance];
                      
                      [self.noDataView showInView:self.view];
                  }
                  
                  [MBProgressHUD hideHUDForView:self.view];
                  
                  [self.tableView reloadData];
              }
              
          }
     
          failure:^(NSError *error){
              
              [self.tableView.mj_header endRefreshing];
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}

#pragma mark - /** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
    NSDictionary *dict = [self.photoDataArr objectAtIndex:index];
    
    
    FindeDetailViewController *findDetailVC = [[FindeDetailViewController alloc] init];
    
    NSString *content = [dict objectForKey:@"ContentViewUrl"];
    
    findDetailVC.content = content;
    
    [self.navigationController pushViewController:findDetailVC animated:YES];
}



#pragma mark -  DHCycleTableViewCellDelegate
-(void)cycleTableViewCellWithLikeAndDislike:(DHCycleTableViewCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
    CZQCycleModel *cycleModel = [self.dataArr objectAtIndex:indexPath.row];
    
    
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    
    [params setObject:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"UserID"]] forKey:@"MemberID"];
    
    [params setObject:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"MasterType"]] forKey:@"UserType"];
    
    [params setObject:cycleModel.ID forKey:@"ActivityID"];
    
    
    
    
    
    
    
    
    
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    
    
    [HTTPTool POSTRaw:Post_Activity_VoteOperate parameters:params
     
              success:^(id responseObject){
                  
                  [MBProgressHUD hideHUDForView:self.view];
                  
                  if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                      
                      
                      if ([cycleModel.IsVote integerValue] == 1) {
                          cycleModel.IsVote = @"0";
                          [cell.goodBtn setImage:@"icon_likes_red"];
                          cycleModel.VoteCount = [NSString stringWithFormat:@"%ld",[cycleModel.VoteCount integerValue] - 1];
                      }else
                      {
                          cycleModel.IsVote = @"1";
                          [cell.goodBtn setImage:@"icon_fabulous"];
                          cycleModel.VoteCount = [NSString stringWithFormat:@"%ld",[cycleModel.VoteCount integerValue] + 1];
                      }
                      
                      
                      
                      
                      [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                      
                      [MBProgressHUD hideHUDForView:self.view];
                  }
                  
              }
     
              failure:^(NSError *error){
                  
                  [MBProgressHUD hideHUDForView:self.view];
              }];
    
    
    
    
}
#pragma mark - UITableViewDataSource

//行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//列数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DHCycleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.iconBtn.tag = 200 + indexPath.row;
    
    [cell.iconBtn addTarget:self action:@selector(cycleDetail:)];
    
    CZQCycleModel *cycleModel = [self.dataArr objectAtIndex:indexPath.row];
    
    cell.delegate = self;
    
    cell.cycleModel = cycleModel;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell  ;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//    return [self.tableView fd_heightForCellWithIdentifier:@"cell" configuration:^(id cell) {
//        [self configureOriCell:cell atIndexPath:indexPath];
//    }];
//}
//
//- (void)configureOriCell:(DHCycleTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
//{
//
//    //从新获取动态高度
//    cell.fd_enforceFrameLayout = NO; // Enable to use "-sizeThatFits:"
//    if (indexPath.row < self.dataArr.count) {
//        cell.cycleModel = self.dataArr[indexPath.row];
//    }
//
//}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当离开某行时，让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CZQCycleDetalVC *cycleD = [CZQCycleDetalVC new];
    
    CZQCycleModel *cycleModel = [self.dataArr objectAtIndex:indexPath.row];
    
    cycleD.cycleModel = cycleModel;
    
    [self.navigationController pushViewController:cycleD animated:YES];
    
}

#pragma mark - 主页
-(void)cycleDetail:(UIButton *)sender
{
    CZQHomePageChileOneVC *cycle = [CZQHomePageChileOneVC new];
    
    CZQCycleModel *cycleModel = [self.dataArr objectAtIndex:sender.tag - 200];
    
    cycle.cycleModel = cycleModel;
    
    cycle.title = [NSString stringWithFormat:@"%@的主页",cycleModel.Name];
    
    [self.navigationController pushViewController:cycle animated:YES];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
